package cloud

import (
	"dgo/MyProj/PrjHttpUtils"
	"dgo/goutils/utils"
	"dgo/plg/plgnet"
	"fmt"
	"gitee.com/ymofen/supervalue"
	"io/ioutil"
	"net/http"
	"sort"
)

func (this *PlgVxApi) onHttpRefreshUsers(w http.ResponseWriter, r *http.Request) {

	req := plgnet.NewPlgHttpRec(w, r)
	//req := PrjHttpUtils.NewHttpRec(w,r)

	req.CheckDecodeBody()

	code, errs := this.RefreshList()
	if errs != "" {
		req.Resp.SetKeyValues("code", code, "msg", errs)
		req.ResponseAsJSON()
		return
	}
	req.Resp.SetKeyValues("code", 0, "msg", "用户信息更新成功!")
	req.ResponseAsJSON()
	return
}

func (this *PlgVxApi) onHttpGetuserlist(w http.ResponseWriter, r *http.Request) {

	req := plgnet.NewPlgHttpRec(w, r)
	//req := PrjHttpUtils.NewHttpRec(w,r)

	req.CheckDecodeBody()

	retdata := wrapper.NewSVObject()
	jsonda, _ := this.Get_User_Openid()
	retdata.SetKeyValue("data", jsonda)

	req.Resp.SetKeyValues("code", 0, "msg", retdata)
	req.ResponseAsJSON()
	return
}

func (this *PlgVxApi) onHttpPushUserMsg(w http.ResponseWriter, r *http.Request) {

	req := plgnet.NewPlgHttpRec(w, r)
	//req := PrjHttpUtils.NewHttpRec(w,r)

	req.CheckDecodeBody()

	noticedata := req.Req.ValueByName("noticedata")

	code, errx := this.PushWxMsg(noticedata)
	if errx != "" {
		req.Resp.SetKeyValues("code", code, "msg", errx)
		req.ResponseAsJSON()
		return
	}
	req.Resp.SetKeyValues("code", 0, "msg", "推送成功！")
	req.ResponseAsJSON()
	return
}

func (this *PlgVxApi) onHttpVxCallback(w http.ResponseWriter, r *http.Request) {

	PrjHttpUtils.AllowCrossAccessSet(w)
	//记录最后报错的信息
	if utils.GoFunCatchException {
		defer utils.PanicHandler()
	}

	req := plgnet.NewPlgHttpRec(w, r)
	//req := PrjHttpUtils.NewHttpRec(w,r)

	defer req.Close()
	req.CheckDecodeBody()
	if req.ReqR.Method == "OPTIONS" {
		return
	}
	signature := req.QryAsString("signature", "")
	timestamp := req.QryAsString("timestamp", "")
	nonce := req.QryAsString("nonce", "")
	echostr := req.QryAsString("echostr", "")
	body4, err8 := ioutil.ReadAll(r.Body)
	if err8 != nil {
		utils.Log.Warn("微信接口请求返回读取异常:", err8.Error())
		req.Resp.SetKeyValues("code", -1, "msg", err8.Error())
		req.ResponseAsJSON()
		return
	}

	body4str := string(body4)
	fmt.Println(body4str)
	Event := this.getXmlstr(body4str, "Event")
	fmt.Println(Event)
	arr := []string{this.vxconfig_token, timestamp, nonce}
	sort.Strings(arr)

	sigres := this.checkSignature(arr[0], arr[1], arr[2])
	//sigres := this.checkSignature(this.vxconfig_token,timestamp,nonce)
	//echostrint,_ := strconv.Atoi(echostr)
	fmt.Println("**************sigres****************")
	fmt.Println("signature:" + signature)
	fmt.Println("sigres:" + sigres)
	fmt.Println("echostr:", echostr)
	//fmt.Println(timestamp)
	//fmt.Println(nonce)
	fmt.Println("************signature****************")

	if signature == sigres {
		if Event == "unsubscribe" || Event == "subscribe" {
			this.RefreshList()
		}
		sb := utils.NewBytesBuilder()
		sb.Appendf(echostr)
		w.Write(sb.Bytes())
	} else {
		req.Resp.SetKeyValues("code", -1, "msg", "签名异常！")
		req.ResponseAsJSON()
		return
	}
}
